﻿Imports CRM.BusinessLayer
Imports System.Windows.Forms
Imports System.Net.Mail

Partial Public Class frmEmailCaseNavigation
    Private Declare Function DrawMenuBar Lib "user32.dll" (ByVal hwnd As IntPtr) As IntPtr
    Private Declare Function GetSystemMenu Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal bRevert As Int32) As IntPtr
    Private Declare Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As IntPtr) As Int32
    Private Declare Function RemoveMenu Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal nPosition As Int32, ByVal wFlags As Int32) As Int32
    Private Const MF_BYPOSITION As Int32 = &H400I
    Private Const MF_REMOVE As Int32 = &H1000I

    Private _Attachment As GlobalCRM.LocalAttachments


    Public Property Attachment() As GlobalCRM.LocalAttachments
        Get
            Return _Attachment
        End Get
        Set(ByVal value As GlobalCRM.LocalAttachments)
            _Attachment = value
        End Set
    End Property


    Private _body As String
    Public Property Body() As String
        Get
            Return _body
        End Get
        Set(ByVal value As String)
            _body = value
        End Set
    End Property

    Private _Subject As String
    Public Property Subject() As String
        Get
            Return _Subject
        End Get
        Set(ByVal value As String)
            _Subject = value
        End Set
    End Property

    Private _To As String
    Public Property ToAddr() As String
        Get
            Return _To
        End Get
        Set(ByVal value As String)
            _To = value
        End Set
    End Property


    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSend.Click
        Dim frmCRMMainDisplay As New frmCRMMainDisplay

        Try
            CommonDataFunctions.EMAILBCCSTRING = txtBCCed.Text
            Dim textRange As TextRange = New TextRange(txtEmailBody.Document.ContentStart, txtEmailBody.Document.ContentEnd)
            Dim shouldEmailBeHtml As String = String.Empty
            Dim configurationAppSettings As System.Configuration.AppSettingsReader = New System.Configuration.AppSettingsReader
            shouldEmailBeHtml = CType(configurationAppSettings.GetValue("shouldEmailBeHtml", GetType(System.String)), String)
            Dim emailBody As String = String.Empty
            If (Not String.IsNullOrEmpty(shouldEmailBeHtml) And (shouldEmailBeHtml.ToUpper = "TRUE" Or shouldEmailBeHtml.ToUpper = "FALSE")) Then
                If (shouldEmailBeHtml.ToUpper() = "FALSE") Then
                    emailBody = textRange.Text
                Else
                    emailBody = textRange.Text.Replace(Environment.NewLine, "<br/>")
                End If
            End If
            CommonDataFunctions.EMAILBODYSTRING = emailBody
            CommonDataFunctions.EMAILSUBJECTSTRING = txtSubject.Text
            frmCRMMainDisplay.blnAmendDataBeforeEmail = False
            frmCRMMainDisplay.blnCancelEmail = False
            Me.Close()
        Catch ex As Exception
        Finally
            frmCRMMainDisplay = Nothing
        End Try
    End Sub

    Private Sub frmEmailCaseNavigation_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Dim frmeditcase As New frmEditCase
        txtFrom.Text = CommonDataFunctions.EMAILFROMSTRING
        txtTo.Text = CommonDataFunctions.EMAILTOSTRING
        txtBCCed.Text = CommonDataFunctions.EMAILBCCSTRING
        Dim textRange As TextRange = New TextRange(txtEmailBody.Document.ContentStart, txtEmailBody.Document.ContentEnd)
        textRange.Text = CommonDataFunctions.EMAILBODYSTRING
        'txtEmailBody. = CommonDataFunctions.EMAILBODYSTRING
        txtSubject.Text = CommonDataFunctions.EMAILSUBJECTSTRING


        Me.txtSubject.Text = Letters.Gblfunc_Process_Bookmarks(txtSubject.Text, GlobalCRM.CurrentCaseID, 1)

        If Strings.Left(textRange.Text, 1) = ">" Then
            textRange.Text = Letters.Gblfunc_Process_Bookmarks(textRange.Text, GlobalCRM.CurrentCaseID, 1) & vbCrLf & vbCrLf & textRange.Text
        Else
            textRange.Text = Letters.Gblfunc_Process_Bookmarks(textRange.Text, GlobalCRM.CurrentCaseID, 1)
        End If
        For Each attachment As GlobalCRM.LocalAttachments.LocalAttachment In GlobalCRM.LocalAttachments.Attachments
            Me.lstAttach.Items.Add(attachment.Name)
        Next

    End Sub

    Private Sub btnBack_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnBack.Click
        Dim frmCRMMainDisplay As New frmCRMMainDisplay
        Try
            frmCRMMainDisplay.blnAmendDataBeforeEmail = True
            Me.Close()

        Catch ex As Exception
        Finally
            frmCRMMainDisplay = Nothing
        End Try
    End Sub

    Private Sub btnRemAttach_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnRemAttach.Click
        Me.lstAttach.Items.RemoveAt(Me.lstAttach.SelectedIndex)
    End Sub

    Private Sub btnAttach_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAttach.Click
        Dim i As Integer
        Dim OpenFileDialog1 As New OpenFileDialog
        Try

        
            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                For i = 0 To OpenFileDialog1.FileNames.Length - 1
                    Me.lstAttach.Items.Add(IO.Path.GetFileName(OpenFileDialog1.FileNames(i).ToString))
                    GlobalCRM.LocalAttachments.AddAttachment(IO.Path.GetFileName(OpenFileDialog1.FileName(i).ToString), OpenFileDialog1.FileNames(i).ToString)
                Next
               
            End If


        Catch ex As Exception
        Finally
            OpenFileDialog1 = Nothing
        End Try
    End Sub
End Class
